﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoVanHocTapData.Interface;
using CoVanHocTapData.Entities;

namespace CoVanHocTapData.Concretes
{
    public class SinhVienMonDuKienMoRepository:ISinhVienMonDuKienMoRepository
    {
        #region property
        DangKyPhuEntities entities;
        #endregion

        #region constructor
        public SinhVienMonDuKienMoRepository()
        {
            entities = new DangKyPhuEntities();
        }
        #endregion


        public void ThemSinhVienMonDuKienMo(string MSSV, long MonDuKienMoID)
        {
            SinhVienMonDuKienMo result = new SinhVienMonDuKienMo()
            {
                MonDuKienMoID = MonDuKienMoID,
                MSSV = MSSV,
                NgayDangKy = DateTime.Now,
                ChoPhepHoc =true
            };
            entities.AddToSinhVienMonDuKienMoes(result);
            entities.SaveChanges();
        }



        public bool KiemTraSinhVienMonDuKienMo(string MSSV, string MonHocID, int HocKyID)
        {
            var result = from m in entities.SinhVienMonDuKienMoes
                         join n in entities.MonDuKienMoes
                         on m.MonDuKienMoID equals n.MonDuKienMoID
                         where m.MSSV.Equals(MSSV)
                         && n.MonHocID.Equals(MonHocID)
                         && n.HocKyID == HocKyID
                         select m;
            if (result == null || result.Count() == 0)
                return false;
            return true;
        }

        public void XoaSinhVienMonDuKienMo(string MSSV, string MonHocID, int HocKyID)
        {
            var result = from m in entities.SinhVienMonDuKienMoes
                         join n in entities.MonDuKienMoes
                         on m.MonDuKienMoID equals n.MonDuKienMoID
                         where m.MSSV.Equals(MSSV)
                         && n.MonHocID.Equals(MonHocID)
                         && n.HocKyID == HocKyID
                         select m;
            foreach (var item in result)
                entities.DeleteObject(item);
            entities.SaveChanges();
        }




        public IQueryable<MonHoc> LayCacMonHocDangKyCuaSinhVien(string MSSV, int hocKyID)
        {
            return (from m in entities.MonDuKienMoes
                        join n in entities.SinhVienMonDuKienMoes
                            on m.MonDuKienMoID equals n.MonDuKienMoID
                            join l in entities.MonHocs
                            on m.MonHocID equals l.MonHocID
                        where n.MSSV.Equals(MSSV)
                        && m.HocKyID.Value==hocKyID
                        select l);
        }
    }
}
